/**
 * grayrabbit Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package com.grayrabbit.slacker.gengerate;

import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.grayrabbit.slacker.config.GenerateConfig;
import com.grayrabbit.slacker.constant.GenerateTypeConstant;
import com.grayrabbit.slacker.constant.PropertyConsant;
import com.grayrabbit.slacker.utils.TemplateProcessUtils;

/**
 * 代码生成工具基础类
 * 
 * @author liyd
 * @version $Id: NormalGenerate.java, v 0.1 2012-5-10 下午2:23:12 liyd Exp $
 */
public abstract class NormalGenerate {

    /** 日志对象 */
    protected final Logger LOG                            = Logger.getLogger(this.getClass());

    /** 包名 */
    protected String       PACKAGE                           = null;

    /** 表名 */
    protected String       TABLE_NAME                        = null;

    /** 需要生成的class名称，含包名，带前后缀，如果有 */
    protected String       TARGET_QUALIFIED_CLASS_NAME       = null;

    /** 实体类名称 含包名 */
    protected String       ENTITY_QUALIFIED_CLASS_NAME       = null;

    /** Dao名称 含包名 */
    protected String       DAO_QUALIFIED_CLASS_NAME          = null;

    /** service接口名称 含包名 */
    protected String       SERVICE_QUALIFIED_CLASS_NAME      = null;                              ;

    /** service实现类名称 含包名 */
    protected String       SERVICE_IMPL_QUALIFIED_CLASS_NAME = null;

    /** manager接口名称 含包名 */
    protected String       MANAGER_QUALIFIED_CLASS_NAME      = null;

    /** manager实现类名称 含包名 */
    protected String       MANAGER_IMPL_QUALIFIED_CLASS_NAME = null;

    /** action类名称 含包名 */
    protected String       ACTION_QUALIFIED_CLASS_NAME       = null;

    /** 当前时间 */
    protected String       GMT_CREATE                        = new Date().toString();

    /**
     * 根据class的绝对名获取相对类名 即去掉包路径
     * 
     * @param qualifiedClassName
     * @return
     */
    protected String getClassName(String qualifiedClassName) {

        if (StringUtils.isBlank(qualifiedClassName)) {
            LOG.info("qualifiedClassName is null");
            return "";
        }

        return StringUtils.substring(qualifiedClassName,
            qualifiedClassName.lastIndexOf(PropertyConsant.PACKAGE_SEPARATOR) + 1);
    }

    /**
     * 代码生成通用实现基础方法
     * 
     * @param generateConfig
     * @param modelMap
     * @return
     */
    public void normalDataInit(GenerateConfig generateConfig) {

        this.PACKAGE = TemplateProcessUtils.getPackageName(generateConfig);

        this.TABLE_NAME = generateConfig.getTableName();

        this.TARGET_QUALIFIED_CLASS_NAME = TemplateProcessUtils
            .getQualifiedClassName(generateConfig);

        this.ENTITY_QUALIFIED_CLASS_NAME = generateConfig
            .getGenerateClass(GenerateTypeConstant.GEN_ENTITY);

        this.DAO_QUALIFIED_CLASS_NAME = generateConfig
            .getGenerateClass(GenerateTypeConstant.GEN_DAO);

        this.SERVICE_QUALIFIED_CLASS_NAME = generateConfig
            .getGenerateClass(GenerateTypeConstant.GEN_SERVICE);

        this.SERVICE_IMPL_QUALIFIED_CLASS_NAME = generateConfig
            .getGenerateClass(GenerateTypeConstant.GEN_SERVICE_IMPL);

        this.MANAGER_QUALIFIED_CLASS_NAME = generateConfig
            .getGenerateClass(GenerateTypeConstant.GEN_MANAGER);

        this.MANAGER_IMPL_QUALIFIED_CLASS_NAME = generateConfig
            .getGenerateClass(GenerateTypeConstant.GEN_MANAGER_IMPL);

        this.ACTION_QUALIFIED_CLASS_NAME = generateConfig
            .getGenerateClass(GenerateTypeConstant.GEN_ACTION);

    }

    /**
     * 根据action类名拆分出action访问路径
     * 
     * @param actionName
     * @return
     */
    protected String getActionName(String actionClassName) {

        Pattern pattern = Pattern.compile("[A-Z]");
        Matcher matcher = pattern.matcher(actionClassName);
        while (matcher.find()) {

            actionClassName = actionClassName.replaceAll(matcher.group(), "_"
                                                                          + matcher.group()
                                                                              .toLowerCase());

        }
        if (actionClassName.startsWith("_")) {
            actionClassName = actionClassName.substring(1, actionClassName.lastIndexOf("_"));
        }
        return actionClassName;
    }

    /**
     * 生成serialVersionUID
     * 
     * @return
     */
    protected String getSerialVersionUID() {
        return String.valueOf(Math.abs(UUID.randomUUID().getMostSignificantBits()));
    }
}
